/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.utilities;

import com.entities.CommentType;
import com.entities.Comments;
import com.entities.PostType;
import com.entities.Posts;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author Min Tun
 */
public class XMLUtilities {
    
    public static void writePostXMLFile(PostType post, String xmlPath) {
        try {
            JAXBContext jaxbc = JAXBContext.newInstance(Posts.class);
            Unmarshaller um = jaxbc.createUnmarshaller();
            Posts posts = new Posts();

            File f = new File(xmlPath);

            if (!f.exists()) {
                posts.getPost().add(post);
            } else {
                if (um.unmarshal(f) != null) {
                    posts = (Posts) um.unmarshal(f);
                    posts.getPost().add(post);
                } else {
                    posts.getPost().add(post);
                }
            }
            Marshaller m = jaxbc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.marshal(posts, new File(xmlPath));
            

        } catch (JAXBException ex) {
            Logger.getLogger(XMLUtilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void writeCommentXMLFile(CommentType comment, String xmlPath){
        try {
            JAXBContext jbc = JAXBContext.newInstance(Comments.class);

            Comments comments = new Comments();

            Unmarshaller um = jbc.createUnmarshaller();

            File f = new File(xmlPath);

            if(f.exists()) {
                if(um.unmarshal(f) != null){
                    comments = (Comments) um.unmarshal(f);
                    comments.getComments().add(comment);
                } else {
                    comments.getComments().add(comment);
                }
            } else {
                comments.getComments().add(comment);
            }
            Marshaller m = jbc.createMarshaller();
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(comments, new File(xmlPath));

        } catch (JAXBException ex) {
            Logger.getLogger(XMLUtilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void approvePost(String id, String xmlPath){
        try {
            JAXBContext jbc = JAXBContext.newInstance(Posts.class);
            Unmarshaller um = jbc.createUnmarshaller();

            Posts posts = new Posts();

            File f = new File(xmlPath);

            posts = (Posts) um.unmarshal(f);

            for (int i = 0; i < posts.getPost().size(); i++) {
                PostType postType = posts.getPost().get(i);
                if(postType.getId().equals(id)){
                    posts.getPost().remove(i);
                }
            }

            Marshaller m = jbc.createMarshaller();
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(posts, new File(xmlPath));
        } catch (JAXBException ex) {
            Logger.getLogger(XMLUtilities.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
